//package monitortest;
//
//
//import gnu.io.CommPortIdentifier;
//import gnu.io.SerialPort;
//import gnu.io.SerialPortEvent;
//import gnu.io.SerialPortEventListener;
//import java.io.InputStream;
//import java.io.OutputStream;
//import java.util.Enumeration;
//import java.util.Scanner;
//import javax.swing.JOptionPane;
//
//
///**
// *
// * @author the Dope Factory
// */
//public class SerialRead extends Thread implements SerialPortEventListener{
//
////    @Override
////    public void serialEvent(SerialPortEvent spe) {
////        throw new UnsupportedOperationException("Not supported yet.");
////    }
//    
//    /** La porta seriale utilizzata per le comunicazioni seriali. */
//  private SerialPort serialPort;
//
//  /** Buffered input stream from the port */
////  private InputStream input;
//  Scanner input;
//  /** The output stream to the port */
////  Scanner output;
////  private OutputStream output;
//
//  /** Milliseconds to block while waiting for port open */
//  private static final int TIME_OUT = 2000;
//
//  /** Default bits per second for COM port. */
//  private static final int DATA_RATE = 9600;
//  
//  
//  /**
//   * Inizializzazione della porta di comunicazione. 
//   */
//  public boolean portInit(String comPort){
//      boolean connesso;
//      
//      CommPortIdentifier portId = null;
//      Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
//      connesso = false;
//      
//      // esegue l' iterazione cercando la porta
//      while (portEnum.hasMoreElements()){
//          CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
//          if (currPortId.getName().equals(comPort)){
//              portId = currPortId;
//              connesso = true;
//              break;
//          }
//      }
//      if (portId == null){
//          JOptionPane.showMessageDialog(null, "Can't find "+comPort);
//          return(false);
//      }
//      try{
//          // open serial port, and use class name for the appName.
//          serialPort = (SerialPort) portId.open(this.getClass().getName(),TIME_OUT);
//          
//          // set port parameters
//          serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT);
//          serialPort.setSerialPortParams(DATA_RATE,
//                                            SerialPort.DATABITS_8,
//                                            SerialPort.STOPBITS_1,
//                                            SerialPort.PARITY_NONE);
//          // open the streams
//          input=new Scanner(serialPort.getInputStream());
////          input = serialPort.getInputStream();
////          output=new Scanner(serialPort.getOutputStream());
////          output = serialPort.getOutputStream();
//          
//          // add event listeners
//          serialPort.addEventListener((SerialPortEventListener) this);
//          serialPort.notifyOnDataAvailable(true);
//      
//      }catch (Exception e){
//        JOptionPane.showMessageDialog(null, "Connection Error: "+ e.toString());
//        connesso = false;
//    }
//    return(connesso);
//  }
//  
//  
//  /**
//  * Questa deve essere chiamata quando si finisce di utilizzare la porta
//  * Previene il bloccaggio della porta in Linux
//  */
//  public synchronized void portClose(){
//      if (serialPort != null){
//          serialPort.removeEventListener();
//          serialPort.close();
//      }
//  }
//  
//  /**
//  * Gestisce l' evento della porta seriale. Riceve il carattere e lo visualizza
//   * 
//   * protocollo:
//   * @=detector, A=switch#1, B=switch#2, C=soglia controller
//   * messaggi possibili:
//   * @R (richiesta valore al detector)
//   * AR (richiesta valore a switch#1)
//   * BR (richiesta valore a switch#2)
//   * AW0 (spegnimento rele#1)
//   * AW1(accensione rele#1)
//   * BW0 (spegnimento rele#2)
//   * BW1(accensione rele#2)
//  */
//    @Override
//    public synchronized void serialEvent(SerialPortEvent event){
////    int i,l,available = 0;
////    boolean interpretato=false;
////    String s,s2;
////    byte chunk[] = null;
////    int caretBit=0;
////    if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE){
////        while(!interpretato){
////          try{
////              available = input.available();
////              chunk = new byte[available];
//////              System.out.println(input);
////               input.read(chunk, 0,available);
////               System.out.println(chunk);
//        String temp=input.nextLine();
//        System.out.println(temp);
//        
//        
////        System.out.println("Serial Event NOW");
////              //SWITCHING STATUS
////              switch(chunk[caretBit]){
////                  case '@':
////                      caretBit++;
////                      switch(chunk[caretBit]){
////                          case 'R':
////                              //richiesta valore al detector
////                              break;
////                          default:
////                              //valore del detector
////                              char[] temp=new char[4];
////                              for(int x=0;x<temp.length;x++){
////                                  temp[x]=(char) chunk[caretBit];
////                              }
////                              String c=temp.toString();                              
////                              Management.consumoIstantaneo=Integer.parseInt(c);
////                              break;
////                      }
////                      break;
////                  case 'A':
////                      caretBit++;
////                      switch(chunk[caretBit]){
////                          case 'R':
////                              char[] temp=new char[4];
////                              for(int x=0;x<temp.length;x++){
////                                  temp[x]=(char) chunk[caretBit];
////                              }
////                              String c=temp.toString();                              
////                              Management.valoreSwitch1interrotto=Integer.parseInt(c);
////                              break;
////                          case 'W':
////                              caretBit++;
////                              switch(chunk[caretBit]){
////                                  case '0':
////                                      //spegnimento relè1
////                                      Management.switch1=false;
////                                      break;
////                                  case '1':
////                                      //accensione relè1
////                                      Management.switch1=true;
////                                      break;
////                                  default:
////                                      break;
////                              }
////                          default:
////                              //richiesta allo switch #1 del consumo
////                              char[] temp2=new char[4];
////                              for(int x=0;x<temp2.length;x++){
////                                  temp2[x]=(char) chunk[caretBit];
////                              }
////                              String c2=temp2.toString();                              
////                              Management.valoreSwitch1=Integer.parseInt(c2);
////                              break;                          
////                      }
////                      
////                      break;
////                  case 'B':
////                      caretBit++;
////                      switch(chunk[caretBit]){
////                          case 'R':
////                              //richiesta allo switch #2 del consumo
////                              char[] temp=new char[4];
////                              for(int x=0;x<temp.length;x++){
////                                  temp[x]=(char) chunk[caretBit];
////                              }
////                              String c=temp.toString();                              
////                              Management.valoreSwitch2interrotto=Integer.parseInt(c);
////                              break;
////                              
////                          case 'W':
////                              caretBit++;
////                              switch(chunk[caretBit]){
////                                  case '0':
////                                      //spegnimento relè2
////                                      Management.switch2=false;
////                                      break;
////                                  case '1':
////                                      //accensione relè2
////                                      Management.switch2=true;
////                                      break;
////                                  default:
////                                      break;
////                              }
////                          default:
////                              char[] temp2=new char[4];
////                              for(int x=0;x<temp2.length;x++){
////                                  temp2[x]=(char) chunk[caretBit];
////                              }
////                              String c2=temp2.toString();                              
////                              Management.valoreSwitch2=Integer.parseInt(c2);
////                              break;
////                      }
////                      break;
////                  case 'a':
////                      
////                      break;
////                  case 'b':
////                      
////                      break;
////                  case 'C':
////                      //valore della soglia del controller
////                              char[] temp=new char[4];
////                              for(int x=0;x<temp.length;x++){
////                                  temp[x]=(char) chunk[caretBit];
////                              }
////                              String c=temp.toString();                              
////                              Management.sogliaController=Integer.parseInt(c);
////                              break;
////                  default:
////                      break;
//          
////          }catch (Exception e){
////            JOptionPane.showMessageDialog(null, "RX error: "+ e.toString());
////          }
//        }
//  }